package com.jzo2o.customer.service.impl;/**
 * @author wl
 * @version 1.0
 * @since 2025-09-17 11:45
 */

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.mapper.WorkerCertificationAuditMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationAudit;
import com.jzo2o.customer.model.dto.request.CertificationAuditReqDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IWorkerCertificationAuditService;
import com.jzo2o.customer.service.IWorkerCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;


/**
 *des:
 *data Created in 2025-09-17 11-45
 */
@Service
public class WorkerCertificationAuditServiceImpl extends ServiceImpl<WorkerCertificationAuditMapper, WorkerCertificationAudit> implements IWorkerCertificationAuditService {
   @Resource
   private IWorkerCertificationService iworkercertificationservice;


    @Override
    public void insertAudit(WorkerCertificationAudit workerCertificationAudit) {

        this.save(workerCertificationAudit);
    }


    /**
     * des:审核认证信息
     * data:2025-09-17 11:45
     */
    @Override
    public void AduitWorkerCertificationAudit(Long id, CertificationAuditReqDTO certificationAuditReqDTO) {
        //获取当前登录人id
        Long userId = UserContext.currentUserId();
        //1.认证通过
        if (certificationAuditReqDTO.getCertificationStatus() == 2){
            //修改信息表
            //根据id修改信息表状态
            Long serveProviderId = getById(id).getServeProviderId();
            WorkerCertification workerCertification = iworkercertificationservice.getById(serveProviderId);
            workerCertification.setCertificationStatus(2);
            workerCertification.setId(serveProviderId);
            iworkercertificationservice.updateById(workerCertification);
            //修改认证表
            WorkerCertificationAudit workerCertificationAudit = getById(id);
            workerCertificationAudit.setId(id);
            workerCertificationAudit.setAuditStatus(1);
            workerCertificationAudit.setCertificationStatus(2);
            workerCertificationAudit.setUpdateTime(LocalDateTime.now());
            workerCertificationAudit.setAuditorId(userId);
            //根据userId查询当前人姓名
            workerCertificationAudit.setAuditorName(UserContext.currentUser().getName());
            workerCertificationAudit.setAuditTime(LocalDateTime.now());
            this.updateById(workerCertificationAudit);
        }
        //2.认证失败
            //修改信息表
        Long serveProviderId = getById(id).getServeProviderId();
        WorkerCertification workerCertification = iworkercertificationservice.getById(serveProviderId);
        workerCertification.setCertificationStatus(3);
        workerCertification.setId(serveProviderId);
        iworkercertificationservice.updateById(workerCertification);
            //修改认证表
        WorkerCertificationAudit workerCertificationAudit = getById(id);
            workerCertificationAudit.setId(id);
            workerCertificationAudit.setAuditStatus(1);
            workerCertificationAudit.setCertificationStatus(3);
            workerCertificationAudit.setUpdateTime(LocalDateTime.now());
            workerCertificationAudit.setAuditorId(userId);
            workerCertificationAudit.setAuditorName(UserContext.currentUser().getName());
            workerCertificationAudit.setAuditTime(LocalDateTime.now());
            workerCertificationAudit.setRejectReason(certificationAuditReqDTO.getRejectReason());
            updateById(workerCertificationAudit);
    }

    /**
     * des:分页查询认证信息
     * data:2025-09-17 11:45
     */
    @Override
    public PageResult<WorkerCertificationAuditResDTO>  pageQuery(WorkerCertificationAuditPageQueryReqDTO workerCertificationAuditPageQueryReqDTO) {
        PageResult<WorkerCertificationAudit> pages = PageHelperUtils.selectPage(workerCertificationAuditPageQueryReqDTO,
                () -> baseMapper.queryList(workerCertificationAuditPageQueryReqDTO));
        List<WorkerCertificationAudit> list = pages.getList();
        List<WorkerCertificationAuditResDTO> workerCertificationAuditResDTOS = BeanUtil.copyToList(list, WorkerCertificationAuditResDTO.class);
        PageResult<WorkerCertificationAuditResDTO> pageResult = new PageResult<>();
        pageResult.setList(workerCertificationAuditResDTOS);
        pageResult.setPages(pages.getPages());
        pageResult.setTotal(pages.getTotal());
        return pageResult;
    }

    @Override
    public String rejectReason(Long userId) {
        //根据当前服务人员id查询拒绝理由
        LambdaQueryWrapper<WorkerCertificationAudit> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WorkerCertificationAudit::getServeProviderId,userId)
                .eq(WorkerCertificationAudit::getAuditStatus,1)
                .eq(WorkerCertificationAudit::getCertificationStatus,3);
        WorkerCertificationAudit one = getOne(queryWrapper);
        return one.getRejectReason();


    }
}
